#! /usr/bin/python
# -*- coding: cp1250 -*-
#sys.path.append(r'C:\x\y')

import glob, re, string, sys, os
import pepe.txt.bigtxt as bt

def join(flsmask_s, csvColDel_s='|', skipHeader_b=0):
    o_fl = open('txt-join.out','w')
    l_fl = open('txt-join_log.CSV','w')
    llenght_h = {}
    ccd_h = {}
    for infile_s in glob.glob(flsmask_s):
        flcnt_n = 1
        for line_s in bt.lines(infile_s):

            # write lines to output
            if flcnt_n == 1 and skipHeader_b == 1:
                pass
            else:
                if len(re.sub('\n','',line_s)) > 0:
                    o_fl.write(line_s)

            #check and record - csv delimiter integrity
            if not csvColDel_s == '':
                ccd_n = string.count(line_s,csvColDel_s)
                if ccd_h.get(ccd_n):
                    ccd_h[ccd_n][0] += 1 
                else:
                    ccd_h[ccd_n] =      [   1,
                                            infile_s,
                                            flcnt_n,
                                            line_s[:-1]]

            #check and record - lines lengths
            if llenght_h.get(len(line_s)):
                llenght_h[len(line_s)][0] += 1
            else:
                llenght_h[len(line_s)] = [  1,
                                            infile_s,
                                            flcnt_n,
                                            line_s[:-1]]
            flcnt_n += 1
    o_fl.close()
    l_fl.write ( 'len;cnt;infile;line no;content\n' )
    for k in sorted(llenght_h.keys(), reverse=True):
        l_fl.write( str(k) + 
                    ';'       + str(llenght_h[k][0]) +
                    ';'    + str(llenght_h[k][1]) +
                    ';'             + str(llenght_h[k][2]) +
                    ';'    + str(llenght_h[k][3]) +
                    '\n'
                    )
    l_fl.write ( 'len;cnt;infile;line no;infile\n' )
    for k in sorted(ccd_h.keys(), reverse=True):
        l_fl.write( str(k) + 
                    ';'      + str(ccd_h[k][0]) +
                    ';'    + str(ccd_h[k][1]) +
                    ';'             + str(ccd_h[k][2]) +
                    ';'    + str(ccd_h[k][3]) +
                    '\n'
                    )
    l_fl.close()
    #os.system('start notepad _join-txt-files_log.xyz')
    return 1

HELP = """Usage: txt-join PATTERN_S DELIMITER_S SKIPHEADER_B
Purpose: Join text files.
Example: txt-join *.csv "|" 1"""

def main():
    print sys.argv
    if len(sys.argv) == 3:
        join(sys.argv[1], sys.argv[2], 0)
    elif len(sys.argv) == 4:
        join(sys.argv[1], sys.argv[2], sys.argv[3])
    else:
        print HELP

if __name__ == "__main__":
    main()
